package org.hzero.business.workflow.builder.impl;

import org.activiti.rest.service.api.engine.variable.RestVariable;
import org.activiti.rest.service.api.runtime.process.ProcessInstanceCreateRequest;
import org.hzero.business.workflow.builder.KnifeWorkflowRequestBuilder;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: AtnProcessInstanceCreateRequestBuilder
 * @Description: 自定义创建工作流参数builder实现类
 * @author: tianhao.luo@hand-china.com
 * @date: 2020/9/4  14:47
 */
public class SimpleRequestBuilder implements KnifeWorkflowRequestBuilder {

    /**
     * 工作流所需参数
     */
    ProcessInstanceCreateRequest request = new ProcessInstanceCreateRequest();
    /**
     * 工作流所需变量
     */
    List<RestVariable> restVariableList = new ArrayList<>();

    @Override
    public KnifeWorkflowRequestBuilder buildProcessDefinitionKey(String processDefinitionKey) {
        this.request.setProcessDefinitionKey(processDefinitionKey);
        return this;
    }

    @Override
    public KnifeWorkflowRequestBuilder buildBusinessKey(String businessKey) {
        this.request.setBusinessKey(businessKey);
        return this;

    }

    @Override
    public KnifeWorkflowRequestBuilder buildVariable(String fieldName, Object value, String valueType, RestVariable.RestVariableScope restVariableScope) {
        RestVariable restVariable = new RestVariable();
        Assert.notNull(fieldName, "请设置流程变量"+fieldName+"的属性名称");
        Assert.notNull(value, "请设置流程变量"+fieldName+"的属性值");
        Assert.notNull(valueType, "请设置流程变量"+fieldName+"的属性类型");
        restVariable.setName(fieldName);
        restVariable.setValue(value);
        restVariable.setType(valueType);
        if (ObjectUtils.isEmpty(restVariableScope)) {
            restVariable.setVariableScope(RestVariable.RestVariableScope.GLOBAL);
        }
        this.restVariableList.add(restVariable);
        this.request.setVariables(this.restVariableList);
        return this;

    }

    @Override
    public ProcessInstanceCreateRequest build() {
        return this.request;
    }
}
